package controller;


import View.*;
import client.ClientManager;
import function.*;
import javafx.scene.control.Button;
import java.io.IOException;
import java.sql.SQLException;

public class Controller {
    private LoginWindow loginWindow;//登录窗口
    private ForgetWindow forgetWindow;//忘记密码窗口
    private RegisterWindow registerWindow;//注册窗口
    private MainWindow mainWindow;//主窗口
    private FillInformationWindow fillInformationWindow;//填写基本信息窗口
    private AddFriendWindow addFriendWindow;//搜索好友窗口
    private AddGroupWindow addGroupWindow;//搜索群聊窗口
    private SelectHeadWindow selectHeadWindow;//选择个人头像窗口
    private SelectGroupHeadWindow selectGroupHeadWindow;//选择群头像窗口
    private CreateGroupWindow createGroupWindow;//创建群聊窗口
    private ClientManager clientManager;//传递信息对象
    public Controller() throws IOException, SQLException {
        loginWindow = new LoginWindow();
        forgetWindow = new ForgetWindow();
        registerWindow = new RegisterWindow();
        mainWindow=new MainWindow();
        clientManager=new ClientManager(mainWindow);
        fillInformationWindow=new FillInformationWindow();
        addFriendWindow =new AddFriendWindow();
        addGroupWindow=new AddGroupWindow();
        selectHeadWindow =new SelectHeadWindow();
        createGroupWindow=new CreateGroupWindow();
        selectGroupHeadWindow=new SelectGroupHeadWindow();
        loginWindow.show();
    }

    public void start() throws IOException, ClassNotFoundException {
        initEvent();
        clientManager.run();
        LoginFunction.loginExec(loginWindow,mainWindow,clientManager);
        RegisterFunction.registerExec(registerWindow,fillInformationWindow);
        ForgetFunction.forgetExec(forgetWindow,loginWindow,clientManager);
        FillInformationFunction.fillInformationExec(fillInformationWindow,loginWindow,registerWindow,selectHeadWindow,clientManager);
        MainFunction.mainExec(addFriendWindow,mainWindow,createGroupWindow,addGroupWindow,selectHeadWindow,clientManager);
        AddFriendFunction.searchFriendExec(addFriendWindow,mainWindow,clientManager);
        AddGroupFunction.searchGroupExec(addGroupWindow,mainWindow,clientManager);
        CreateGroupFunction.createGroupExec(mainWindow,createGroupWindow,selectGroupHeadWindow,clientManager);
        loginWindow.show();
    }
    //获取组件对象的方法
    public Object $(window window, String id) {
        return (Object) window.getRoot().lookup("#" + id);
    }

    //初始化，设定各个界面跳转的事件
    public void initEvent() {
        ((Button) $(loginWindow, "registerButton")).setOnAction(event -> {
            loginWindow.hide();
            loginWindow.clear();
            registerWindow.show();
        });
        ((Button) $(registerWindow, "backButton")).setOnAction(event -> {
            registerWindow.hide();
            registerWindow.clear();
            loginWindow.show();
        });
        ((Button) $(loginWindow, "forgetButton")).setOnAction(event -> {
            loginWindow.hide();
            loginWindow.clear("pswText");
            forgetWindow.show();

        });
        ((Button) $(forgetWindow, "cancelButton")).setOnAction(event -> {
            forgetWindow.hide();
            forgetWindow.clear();
            loginWindow.show();
        });
        ((Button)$(fillInformationWindow,"backButton")).setOnAction(event->{
            mainWindow.show();
            fillInformationWindow.hide();
        });
        ((Button)$(addFriendWindow,"cancelButton")).setOnAction(event->{
            mainWindow.show();
            addFriendWindow.hide();
        });
        ((Button)$(addFriendWindow,"backButton")).setOnAction(event->{
            mainWindow.show();
           addFriendWindow.hide();
        });
        ((Button)$(selectHeadWindow,"backButton")).setOnAction(event->{
            selectHeadWindow.hide();
            fillInformationWindow.show();
        });
    }
}